<template>
  <div class="app">
    <header>
      <h3>购物车</h3>
    </header>
    <section>
      <MyMain
        v-for="item in list"
        :key="item.title"
        :item="item"
        @countNum="countNum"
        @changeSelect="changeSelect"
      />
    </section>
   <MyFooter
    @changecheckAll="changecheckAll"
    :checkAll="checkAll"
    :totalCount="totalCount"
    :totalPrice="totalPrice"
   />
  </div>
</template>
<script>
import axios from 'axios'
import MyFooter from './components/myFooter.vue'
import MyMain from './components/myMain.vue'
export default {
  name: 'App',
  data () {
    return {
      list: [],
      checkAll: false
    }
  },
  // 注册组件
  components: {
    MyMain,
    MyFooter
  },
  // 函数创建
  methods: {
    // 修改数量
    countNum (name, id) {
      this.list.forEach(item => {
        if (item.title === name) {
          item.num += id
        }
      })
    },
    // 全选复选框
    changecheckAll () {
      this.checkAll = !this.checkAll
      // 全选后让列表复选框全选
      this.list.forEach(item => {
        item.select = this.checkAll
      })
    },
    // 修改每一项的复选框状态
    changeSelect (name) {
      this.list.forEach(item => {
        if (item.title === name) {
          item.select = !item.select
        }
      })
    }
  },
  // 事件监听
  watch: {
    list: {
      handler () {
        // 监听list中input的变化
        this.checkAll = this.list.every(item => item.select)
      },
      deep: true
    }
  },
  // 计算属性 （用来计算总数和总价）
  computed: {
    totalCount () {
      return this.list.reduce((pev, val) => {
        return pev + (val.select ? val.num : 0)
      }, 0)
    },
    totalPrice () {
      return this.list.reduce((pev, val) => {
        return pev + (val.select ? val.num * val.money : 0)
      }, 0)
    }
  },
  // 挂载阶段获取数据
  mounted () {
    axios.post('/list').then(res => {
      console.log(res.data)
      this.list = res.data
    })
  }
}
</script>
<style lang="less">
*{
  padding: 0;
  margin: 0;
  list-style: none;
  box-sizing: border-box;
}
html,body{
  width: 100%;
  height: 100%;
}
.app{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  font-family: "楷体";
}
header{
  width: 100%;
  height: 50px;
  background: rgb(199, 199, 199);
  h3{
    text-align: center;
    line-height: 50px;
    font-size: 18px;
  }
}
section{
  flex: 1;
  overflow: hidden;
  overflow-y: scroll;
  img{
    width: 80px;
    height: 80px;
  }
  >div{
    display: flex;
    align-items: center;
    margin:10px;
    >p{
      margin: 0 5px;
    }
  }
  .right{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    >p{
      margin: 8px;
    }
  }
  .btns{
    text-align: right;
    button{
      width: 20px;
      height: 20px;
      border: 0;
    }
    i{
      font-style: normal;
      margin: 0 3px;
    }
  }
}
.old{
  text-decoration: line-through;
  font-size: 14px;
  color: #ccc;
}
.new{
  color: red;
  font-weight: bolder;
}
footer{
  width: 100%;
  height: 50px;
  background: rgb(199, 199, 199);
  display: flex;
  align-items: center;
  .all{
    width: 100px;
    text-align: center;
  }
  .money{
    flex: 1;
    text-align: right;
    padding-right: 10px;
    i{
      font-style: normal;
    }
  }
  .go{
    height: 100%;
    background: red;
    color: #fff;
    width: 140px;
    text-align: center;
    line-height: 50px;
    i{
      font-style: normal;
    }
  }
}
</style>
